package Concorrencia;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

import JDBC.Constants;
import JDBC.DBConnection;


public class Evento {
	
	private int idEvento;
	private String nomeEvento;
	private Date data;
	private Time hora;
	private List<String> bilhetes;
	
	private Evento(int idEvento, String nomeEvento, Date data, Time hora) {
		this.idEvento = idEvento;
		this.nomeEvento = nomeEvento;
		this.data = data;
		this.hora = hora;
	}

	public int getIdEvento() {
		return idEvento;
	}

	public String getNomeEvento() {
		return nomeEvento;
	}

	public String getDataString() {
		return data == null ? "Sem data agendada" : data.toGMTString();
	}

	public String getHoraString() {
		return hora == null ? "Sem hora agendada" : hora.toGMTString();
	}

	public List<String> getBilhetes() {
		return bilhetes;
	}

	public void setBilhetes(List<String> bilhetes) {
		this.bilhetes = bilhetes;
	}
	
	
	public static List<Evento> obterEventos() {
		
		String query = "select idEvento as id, nomeEvento as nome, data, hora from eventoOferecido";
		
		DBConnection dbConnection = new DBConnection(Constants.DB_NAME, Constants.USER, Constants.PASSWORD, false, null);

        JDBC.DBConnection.ConnectionHandler ch = DBConnection.getConnectionHandlerInstance();
        dbConnection.returnResultSetSelectStatement(query, 1, ch);
        
        List<Evento> eventos = new ArrayList<Evento>();
        try {
            while(ch.r.next()) {
            	Evento evento = new Evento(ch.r.getInt("id"), ch.r.getString("nome"), ch.r.getDate("data"), ch.r.getTime("hora"));
                eventos.add(evento);
            }
            ch.r.close();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        dbConnection.closeDatabase();
        
    	return eventos;
	}

	public static List<String> obterLugaresEvento(int idEvento) {
		
		String query ="select numeroLugar, estado from Bilhetes where idEvento = '"+idEvento+"'";
		
		DBConnection dbConnection = new DBConnection(Constants.DB_NAME, Constants.USER, Constants.PASSWORD, false, null);

        JDBC.DBConnection.ConnectionHandler ch = DBConnection.getConnectionHandlerInstance();
        dbConnection.returnResultSetSelectStatement(query, 1, ch);
        
        List<String> lugares = new ArrayList<String>();
        try {
            while(ch.r.next()) {
            	if(ch.r.getInt("estado") == 0) {
            		lugares.add(ch.r.getString("numeroLugar"));
            	}
            }
            ch.r.close();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        dbConnection.closeDatabase();
        
    	return lugares;
			
	}
	
	public static boolean reservarLugarEvento(int idEvento, String lugar) {
		
		String query ="updateselect numeroLugar, estado from Bilhetes where idEvento = '"+idEvento+"'";
		
		DBConnection dbConnection = new DBConnection(Constants.DB_NAME, Constants.USER, Constants.PASSWORD, false, null);

        JDBC.DBConnection.ConnectionHandler ch = dbConnection.getConnectionHandlerInstance();
        dbConnection.updateStatement(query, ch);
        
        System.out.println((String)ch.returnStatus);
        
        
        
		return false;
	}

}
